package com.healthcloud.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.healthcloud.annotation.SysLog;
import com.healthcloud.entity.HlHospitalEntity;
import com.healthcloud.entity.MsgVo;
import com.healthcloud.service.HlHospitalService;
import com.healthcloud.utils.ApiRes;
import com.healthcloud.utils.PageParam;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.healthcloud.entity.HospitalMasterConversionEntity;
import com.healthcloud.service.HospitalMasterConversionService;
import springfox.documentation.annotations.ApiIgnore;


/**
 * 医院后台中间转换服务表
 *
 * @author yucl
 * @email yucailong1111@126.com
 * @date 2018-03-05 10:15:51
 */
@ApiIgnore
@Controller
@RequestMapping("hospitalconversion")
public class HospitalMasterConversionController {
	@Autowired
	private HospitalMasterConversionService hospitalMasterConversionService;

	@Autowired
    private HlHospitalService hlHospitalService;
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("hospitalconversion:list")
	public String list(@RequestParam Map<String, Object> params){
	    return "bus/hospitalconversion/hospitalconversionmgr";
	}

	@RequiresPermissions("hospitalconversion:list")
	@RequestMapping(value = "/search")
	@ResponseBody
    public MsgVo  searchHospitalMasterConversion(HospitalMasterConversionEntity bean,Model model, @ModelAttribute("pageParam")PageParam pageParam){
        List<HospitalMasterConversionEntity> list = new ArrayList<>();
        int count;
	    if(!"".equals(bean.getMyHospName()) && bean.getMyHospName() != null) {
            list = hospitalMasterConversionService.queryByName(bean.getMyHospName(),pageParam);
            count = hospitalMasterConversionService.queryCount(bean.getMyHospName());
	    }else {
            list = hospitalMasterConversionService.queryList(bean, pageParam);
            count = hospitalMasterConversionService.queryTotal(bean);
        }

        for(HospitalMasterConversionEntity entity : list) {
            if("".equals(entity.getMyHospId()) || entity.getMyHospId() == null) {
                entity.setMyHospName("");
            }else {
                //设置医卫士医院名
                 if(hlHospitalService.queryObject(Long.valueOf(entity.getMyHospId())) != null ) {
                    entity.setMyHospName(hlHospitalService.queryObject(Long.valueOf(entity.getMyHospId())).getHospName());
                }else {
                    entity.setMyHospName("");
                }
            }
        }
        return new MsgVo(list,count,count,pageParam.getsEcho());
    }

	/**
	 * 新增页
	 */
	@RequestMapping("/tosave")
	@RequiresPermissions("hospitalconversion:save")
    public String toSave(Model model) {
        return "bus/hospitalconversion/addOrUpdatehospitalconversion";
    }

	/**
	 * 保存
	 */
    @SysLog("保存HospitalMasterConversion")
    @RequestMapping("/save")
    @ResponseBody
    public ApiRes save(HospitalMasterConversionEntity entity) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        entity.setCreateTime(df.format(new Date()));
        entity.setUpdateTime(df.format(new Date()));
        hospitalMasterConversionService.save(entity);
        return ApiRes.SUCCESS;
    }

	/**
	 * 修改页
	 */
	@RequestMapping("/toupdate")
    @RequiresPermissions("hospitalconversion:update")
    public String toUpdate(@Param("id") Integer id, Model model) {

        HospitalMasterConversionEntity entity = hospitalMasterConversionService.queryObject(id);
        model.addAttribute("entity", entity);

        if (entity.getMyHospId() == null || "".equals(entity.getMyHospId())) {
            model.addAttribute("hl_hospital",null);
        }else {
            HlHospitalEntity hlHospitalEntity = hlHospitalService.queryObject(Long.valueOf(entity.getMyHospId()));
            model.addAttribute("hl_hospital",hlHospitalEntity);
        }
        return "bus/hospitalconversion/addOrUpdatehospitalconversion";
    }

    /**
     * 修改
     */
    @SysLog("修改HospitalMasterConversion")
    @RequestMapping("/update")
    @ResponseBody
    public ApiRes update(HospitalMasterConversionEntity entity) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        entity.setUpdateTime(df.format(new Date()));
        hospitalMasterConversionService.update(entity);
        return ApiRes.SUCCESS;
    }


    /**
     * 删除
     */
    @SysLog("删除HospitalMasterConversion")
    @RequestMapping("/delete")
    @RequiresPermissions("hospitalconversion:delete")
    @ResponseBody
    public ApiRes delete(Integer[] ids) {
        hospitalMasterConversionService.deleteBatch(ids);
        return ApiRes.SUCCESS;
    }

}
